<template>
  <el-dialog title="修改密码" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="旧密码：" prop="old_password">
        <el-input v-model="ruleForm.old_password" show-password></el-input>
      </el-form-item>
      <el-form-item label="新密码：" prop="new_password">
        <el-input v-model="ruleForm.new_password" show-password></el-input>
      </el-form-item>
      <el-form-item label="确认密码：" prop="again_new_password">
        <el-input v-model="ruleForm.again_new_password" show-password></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="eidtPassWord">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isPassword } from '@/utils/validate'
  import { editUesrPwd } from '@/api/user'
  export default {
    data() {
      const validatePassword = (rule, value, callback) => {
        if (!isPassword(value ?? '')) {
          callback(new Error('密码不能少于6位'))
        } else if (!/^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{6,}$/.test(value)) {
          callback(new Error('密码必须包含数字和字母，不可使用特殊字符'))
        } else {
          callback()
        }
      }
      const validateAgainPassword = (rule, value, callback) => {
        if (!isPassword(value ?? '')) {
          callback(new Error('密码不能少于6位'))
        } else if (value !== this.ruleForm.new_password) {
          callback(new Error('两次密码输入不一致'))
        } else {
          callback()
        }
      }
      return {
        ruleForm: {},
        dialogVisible: false,
        rules: {
          old_password: [{ required: true, validator: validatePassword, trigger: 'blur' }],
          new_password: [{ required: true, validator: validatePassword, trigger: 'blur' }],
          again_new_password: [{ required: true, validator: validateAgainPassword, trigger: 'blur' }],
        },
      }
    },
    methods: {
      open(info) {
        this.ruleForm = { ...info }
        this.dialogVisible = true
      },
      eidtPassWord() {
        console.log(this.ruleForm)
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            this.$confirm('确定修改账号密码?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
            }).then(async () => {
              await editUesrPwd(this.ruleForm).then((data) => {
                if (data.code == 200) {
                  this.$notify({
                    title: '成功',
                    message: '修改成功,请重新登录账号',
                    type: 'success',
                  })
                  this.dialogVisible = false
                  setTimeout(() => {
                    this.$store.dispatch('user/logout')
                    if (recordRoute) {
                      const fullPath = this.$route.fullPath
                      this.$router.push(`/login?redirect=${fullPath}`)
                    } else {
                      this.$router.push('/login')
                    }
                  }, 3000)
                } else {
                  this.$message.error(data.msg)
                }
              })
            })
          } else {
            return false
          }
        })
      },
      handleClose() {
        this.$emit('close')
        this.dialogVisible = false
      },
    },
  }
</script>

<style></style>
